﻿#include "GraphicsScene.h"
#include <GraphicsImageItem.h>
#include <GraphicsView.h>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>


GraphicsScene::GraphicsScene(GraphicsView* view, QObject* parent)
	:QGraphicsScene(parent)
	, m_view(view)
{
	// 当graphicsView的宽高小于m_scene的宽高，场景的原点和视图原点重合
	int sceneSize = 30000;
	setSceneRect(0, 0, sceneSize, sceneSize);
	m_undoStack = new QUndoStack(this);
	m_view->setScene(this);
}

GraphicsScene::~GraphicsScene()
{
	for (QGraphicsItem* item : items()) {
		removeItem(item);
		delete item;
	}
}

void GraphicsScene::setSelectItem(GraphicsBaseItem* baseItem)
{
	baseItem->setSelected(true);
	QList<QGraphicsItem*> items = selectedItems();
	for (QGraphicsItem* item : items)
	{
		if (item == baseItem)
			continue;
		item->setSelected(false);
	}
}

void GraphicsScene::addBaseItem(GraphicsBaseItem* baseItem)
{
	m_baseItemChilds.push_back(baseItem);
	addItem(baseItem);
}

void GraphicsScene::pushCommand(QUndoCommand* command)
{
	m_undoStack->push(command);
}

void GraphicsScene::undo()
{
	m_undoStack->undo();
}

void GraphicsScene::redo()
{
	m_undoStack->redo();
}

void GraphicsScene::clearUndoStack()
{
	if (!m_undoStack)
		return;
}

void GraphicsScene::drawBackground(QPainter* painter, const QRectF& rect)
{
	const int w = width();
	const int h = height();
	painter->setPen(Qt::red);
	painter->setBrush(QColor(26, 26, 26));
	painter->drawRect(0, 0, w, h);
}

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
	m_lastScenePos = mouseEvent->scenePos();
	printInfo();
	QGraphicsScene::mouseMoveEvent(mouseEvent);
}

void GraphicsScene::isEnableItems(bool isEnable)
{
	for (GraphicsBaseItem* child : m_baseItemChilds) {
		child->setEnabled(isEnable);
	}
}

void GraphicsScene::printInfo()
{
	// 场景鼠标坐标
	emit sigSceneMousePos(m_lastScenePos);
	for (GraphicsBaseItem* child : m_baseItemChilds) {
		if (GraphicsItemType::Box_Enum == child->getItemData().getBaseItemType()) {
			// 扩图鼠标坐标
			const QPointF boxMousePos = child->mapFromScene(m_lastScenePos);
			emit sigBoxMousePos(boxMousePos);

			const qreal s = child->scale();
			const QPoint boxPos = child->pos().toPoint();
			const QRect boxScaleRect = child->getItemData().getScaleRect().toRect();
			const QRect boxRect = child->getItemData().getRealRect().toRect();
			const QString boxInfo = QString("左上(%1,%2),"
				"右上(%3,%4),"
				"右下(%5,%6),"
				"左下(%7,%8),"
				"放大尺寸(%9x%10),"
				"实际尺寸(%11x%12),"
				"放大系数:%13")
				.arg(boxScaleRect.topLeft().x())
				.arg(boxScaleRect.topLeft().y())
				.arg(boxScaleRect.topRight().x())
				.arg(boxScaleRect.topRight().y())
				.arg(boxScaleRect.bottomRight().x())
				.arg(boxScaleRect.bottomRight().y())
				.arg(boxScaleRect.bottomLeft().x())
				.arg(boxScaleRect.bottomLeft().y())
				.arg(boxScaleRect.width())
				.arg(boxScaleRect.height())
				.arg(boxRect.width())
				.arg(boxRect.height())
				.arg(child->getItemData().getItemScale());
			// 扩图边界信息
			emit sigBoxInfo(boxInfo);
			QList<QGraphicsItem*> items = child->childItems();
			for (QGraphicsItem* item : items) {
				GraphicsImageItem* imageItem = dynamic_cast<GraphicsImageItem*>(item);
				if (!imageItem)
					continue;
				const QRect imageScaleRect = imageItem->getItemData().getScaleRect().toRect();
				const QRect imageRect = imageItem->getItemData().getRealRect().toRect();
				const QString imageInfo = QString("左上(%1,%2),"
					"右上(%3,%4),"
					"右下(%5,%6),"
					"左下(%7,%8),"
					"放大尺寸(%9x%10),"
					"实际尺寸(%11x%12),"
					"中心坐标(%13,%14),"
					"放大系数:%15")
					.arg(imageScaleRect.topLeft().x())
					.arg(imageScaleRect.topLeft().y())
					.arg(imageScaleRect.topRight().x())
					.arg(imageScaleRect.topRight().y())
					.arg(imageScaleRect.bottomRight().x())
					.arg(imageScaleRect.bottomRight().y())
					.arg(imageScaleRect.bottomLeft().x())
					.arg(imageScaleRect.bottomLeft().y())
					.arg(imageScaleRect.width())
					.arg(imageScaleRect.height())
					.arg(imageRect.width())
					.arg(imageRect.height())
					.arg(imageScaleRect.center().x())
					.arg(imageScaleRect.center().y())
					.arg(imageItem->getItemData().getItemScale());
				// 图片边界信息
				emit sigImageInfo(imageInfo);
			}
		}
	}
}

GraphicsView* GraphicsScene::getView()
{
	return m_view;
}

void GraphicsScene::setCenterItem(GraphicsBaseItem* centerItem)
{
	m_centerItem = centerItem;
	if (m_view) {
		m_view->centerOn(m_centerItem);
	}
}

